﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MO.ModelingClient
{
    public partial class FormForm : Form
    {
        public FormForm()
        {
            InitializeComponent();
            tableControl1.AfterDelete = tableControl_AfterDelete;
        }

        private void tableControl_AfterDelete(string key)
        {
            DbOperation.DB.ExecuteQuery("delete from Form_Item where Form_Code = {0} ", key);
        }

        private void FormForm_Shown(object sender, EventArgs e)
        {
            DataConfig config = new DataConfig();
            config.TableName = "Form_Define";
            config.KeyField = "Form_Code";
            config.TitleField = "Form_Name";
            config.AddItem(new ItemData("Form_Code", "编码"));
            config.AddItem(new ItemData("Form_Name", "名称"), true);
            config.AddItem(new ItemData("Form_Description", "描述"));

            ItemData formItem = new ItemData("Form_Item", "表单数据项")
            {
                Type = DataType.ChildTable,
                IsDd = false,
                ChildConfig = GetFormItemConfig()
            };
            config.AddItem(new ItemData("ED_Code", "关联实体")
            {
                Type = DataType.Enum,
                Enums = DataManager.FindAllEntity().ToDictionary(k => k.Code, v => v.Name),
                ControlValueChanged = delegate(object value)
                {
                    if (value == null || value.ToString() == "")
                        return;
                    var enums = DataManager.FindFieldByEntityCode(value.ToString()).ToDictionary(k => k.Code, v => v.Name);
                    enums[""] = "";
                    formItem.ChildConfig.GetItemData("FD_Code").Enums = enums;
                    var titleItem = config.GetItemData(config.TitleField);
                    if (string.IsNullOrEmpty(titleItem.ItemControl.GetValue() as string))
                    {
                        titleItem.ItemControl.SetValue(DataManager.FindEntityByEntityCode(value.ToString()).Name);
                    }
                }
            });

            config.AddItem(formItem);
            config.AddItem(new ItemData("Form_View_Template", "界面模版"));
            config.AddItem(new ItemData("Form_View_Script", "界面脚本"));
            config.AddItem(new ItemData("Form_Submit_Script", "服务器提交脚本")
            {
                GetBaseTextEditForm = delegate()
                {
                    string entityCode = config.GetItemData("ED_Code").ItemControl.GetValue() as string;
                    List<TreeNode> list = new List<TreeNode>();
                    if (!string.IsNullOrEmpty(entityCode))
                    {
                        TreeNode entityNode = new TreeNode(DataManager.FindEntityByEntityCode(entityCode).Name);
                        entityNode.Tag = entityCode;
                        list.Add(entityNode);
                        var fields = DataManager.FindFieldByEntityCode(entityCode);
                        foreach (var field in fields)
                        {
                            entityNode.Nodes.Add(new TreeNode(field.Name) { Tag = entityCode + "." + field.Code });
                        }
                        entityNode.ExpandAll();
                        list.Add(SysTreeNode.GetSysTreeNode());
                    }
                    return new ScriptEditForm(list.ToArray());
                }
            });
            tableControl1.Init(config);
        }
        DataConfig GetFormItemConfig()
        {
            DataConfig config = new DataConfig();
            config.TableName = "Form_Item";
            config.KeyField = "FI_Code";
            config.TitleField = "FI_Name";
            config.OrderField = "FI_Order";
            config.AddItem(new ItemData("FI_Code", "编码"));
            config.AddItem(new ItemData("FI_Name", "名称"), true);
            config.AddItem(new ItemData("FI_Description", "描述"));
            config.AddItem(new ItemData("FD_Code", "关联指标")
            {
                Type = DataType.Enum,
                ControlValueChanged = delegate(object value)
                {
                    var titleItem = config.GetItemData(config.TitleField);
                    if (string.IsNullOrEmpty(titleItem.ItemControl.GetValue() as string))
                    {
                        titleItem.ItemControl.SetValue(DataManager.FindFieldByFieldCode(value.ToString()).Name);
                    }
                }
            });
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict[ControlType.Text] = "文本框";
            dict[ControlType.Textarea] = "多行文本框";
            dict[ControlType.Label] = "文本显示框";
            dict[ControlType.Number] = "数字";
            dict[ControlType.Bool] = "布尔";
            dict[ControlType.Time] = "时间";
            dict[ControlType.Date] = "日期";
            dict[ControlType.Radio] = "单选框";
            dict[ControlType.Select] = "下拉框";
            dict[ControlType.Checkbox] = "多选框";
            dict[ControlType.Img] = "图片";
            dict[ControlType.Sound] = "语音";
            dict[ControlType.Hide] = "隐藏";
            config.AddItem(new ItemData("FI_Control_Type", "控件类型") { Type = DataType.Enum, Enums = dict });
            config.AddItem(new ItemData("FI_Control_Sources", "控件数据源"));
            config.AddItem(new ItemData("FI_Check_Condition", "验证条件"));
            config.AddItem(new ItemData("FI_Default_Value", "控件默认值"));
            config.AddItem(new ItemData("FI_Order", "排序")
            {
                Type = DataType.Number,
                CreateControlDelegate = delegate(ItemBaseControl control)
                {
                    control.SetValue(config.TableControl.GetCount() * 2);
                }
            });
            config.AddItem(new ItemData("FI_Control_Config", "控件高级配置"));
            return config;
        }
    }
}
